package com.hualude.roadsign.repository;

import com.hualude.roadsign.dto.DTRequestDto;
import com.hualude.roadsign.dto.SolarEnergyDto;
import com.hualude.roadsign.dto.SolarEnergyFaultDto;
import com.hualude.roadsign.dto.MapInfoDto;
import com.hualude.roadsign.model.SolarEnergyEntity;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;

import java.util.List;

/**
 * Created by shao-yikun on 2017/7/11.
 */
public interface SolarEnergyRepository extends CrudRepository<SolarEnergyEntity, Integer> {
    /**
     * @Description: 太阳能产品DT展示
     * @Author: shao-yikun
     * @Date: 2017/7/11 下午10:51
     */
    public List<SolarEnergyDto> getSolarEnergy4DT(DTRequestDto dtRequestDto,
                                                  String actorIds,
                                                  Integer orgId,
                                                  Boolean audit,
                                                  String startRoad,
                                                  String endRoad,
                                                  String roadName,
                                                  Integer alarmBinding) throws Exception;

    /**
     * @Description: 太阳能产品DT数量
     * @Author: shao-yikun
     * @Date: 2017/7/11 下午11:01
     */
    public Integer getSolarEnergy4DTCount(DTRequestDto dtRequestDto,
                                          String actorIds,
                                          Integer orgId,
                                          Boolean audit,
                                          String startRoad,
                                          String endRoad,
                                          String roadName,
                                          Integer alarmBinding) throws Exception;

    /**
     * @Description: 太阳能产品APP展示
     * @Author: shao-yikun
     * @Date: 2017/7/12 下午2:52
     */
    public List<SolarEnergyDto> getSolarEnergy4APP(String actorIds,
                                                   Integer orgId,
                                                   Integer mCount,
                                                   Integer mSize,
                                                   String city,
                                                   String startRoad,
                                                   String endRoad,
                                                   String roadName,
                                                   String alarmType) throws Exception;

    /**
     * @Description: 删除太阳能产品
     * @Author: shao-yikun
     * @Date: 2017/7/12 下午2:54
     */
    @Modifying
    @Query(value = "delete from SolarEnergyEntity where solarEnergyId = ?1")
    public Integer deleteSolarEnergy(Integer solarEnergyId) throws Exception;

    /**
     * @Description: 修改太阳能产品
     * @Author: shao-yikun
     * @Date: 2017/7/12 下午2:55
     */
    @Modifying
    @Query(value = "update SolarEnergyEntity set projectNumber = ?2, solarEnergyLongitude = ?3, " +
            "solarEnergyLatitude = ?4, solarEnergyProvince = ?5, solarEnergyCity = ?6, " +
            "solarEnergyArea = ?7, solarEnergyRoadName = ?8, solarEnergyStartRoad = ?9, " +
            "solarEnergyEndRoad = ?10, solarEnergyType = ?11, solarEnergyStyle = ?12, " +
            "solarEnergyMaterial = ?13, solarEnergyBrand = ?14, solarEnergyPhoto = ?15, " +
            "solarEnergyInstallTime = ?16, solarEnergyInstallOrg = ?17, solarEnergyPeriod = ?18, " +
            "solarEnergyServiceLife = ?19, solarEnergyRemark = ?20, solarEnergyCreatorId = ?21, " +
            "solarEnergyModifierId = ?22, solarEnergyOrgId = ?23, solarEnergyMainOrgId = ?24, " +
            "solarEnergyIsComplete = 0 where solarEnergyId = ?1")
    public Integer updateSolarEnergy(Integer solarEnergyId,
                                     String projectNumber,
                                     String solarEnergyLongitude,
                                     String solarEnergyLatitude,
                                     String solarEnergyProvince,
                                     String solarEnergyCity,
                                     String solarEnergyArea,
                                     String solarEnergyRoadName,
                                     String solarEnergyStartRoad,
                                     String solarEnergyEndRoad,
                                     Integer solarEnergyType,
                                     Integer solarEnergyStyle,
                                     Integer solarEnergyMaterial,
                                     String solarEnergyBrand,
                                     String solarEnergyPhoto,
                                     String solarEnergyInstallTime,
                                     String solarEnergyInstallOrg,
                                     Integer solarEnergyPeriod,
                                     Integer solarEnergyServiceLife,
                                     String solarEnergyRemark,
                                     Integer solarEnergyCreatorId,
                                     Integer solarEnergyModifierId,
                                     Integer solarEnergyOrgId,
                                     Integer solarEnergyMainOrgId) throws Exception;

    /**
     * @Description: 审核太阳能产品
     * @Author: shao-yikun
     * @Date: 2017/7/20 下午6:50
     */
    @Modifying
    @Query(value = "update SolarEnergyEntity set solarEnergyIsComplete = 1 where solarEnergyId = ?1")
    public Integer auditSolarEnergy(Integer solarEnergyId) throws Exception;

    /**
     * @Description: 太阳能详情
     * @Author: pan-jianan
     * @Date: 2017/7/12 18:46
     */
    @Query(value = "select new com.hualude.roadsign.dto.SolarEnergyFaultDto(soe.solarEnergyId,soe.solarEnergyPhoto,soe.solarEnergyLongitude," +
            "soe.solarEnergyLatitude,soe.solarEnergyProvince,soe.solarEnergyCity,soe.solarEnergyArea,soe.solarEnergyRoadName," +
            "soe.solarEnergyStartRoad,soe.solarEnergyEndRoad,soe.solarEnergyType,soe.solarEnergyStyle,soe.solarEnergyMaterial," +
            "soe.solarEnergyInstallOrg,soe.solarEnergyCreatorId,soe.solarEnergyOrgId,soe.solarEnergyMainOrgId) " +
            "from SolarEnergyEntity soe where soe.solarEnergyId=?1")
    public SolarEnergyFaultDto getSolarEnergyInfo2Fault(Integer solarEnergyId) throws Exception;

    /**
     * @Description: 获取路段By路名
     * @Author: jiang-weirong
     * @Date: 2017/7/13 21:17
     */
    public List<String> getRoadByRoadName(String roadName, Integer actorId, Integer orgId) throws Exception;

    /**
     * @Description: 通过起始路段搜索截止路段
     * @Author: jiang-weirong
     * @Date: 2017/7/13 21:34
     */
    public List<String> getSectionByRoadName(String roadName, Integer actorId, Integer orgId) throws Exception;

    /**
     * @Description: 获取太阳能坐标4MAP
     * @Author: pan-jianan
     * @Date: 2017/7/14 15:59
     */
    public List<SolarEnergyFaultDto> getSolarCoordinate4Map(Integer orgId, Integer actorId) throws Exception;

    /**
     * @Description: 根据太阳能坐标获取信息
     * @Author: pan-jianan
     * @Date: 2017/7/14 16:16
     */
    public List<SolarEnergyFaultDto> getSolarEnergyByXY(String solarEnergyLongitude, String solarEnergyLatitude) throws Exception;

    /**
     * @Description: 判断是否可上报
     * @Author: jiang-weirong
     * @Date: 2017/7/14 17:16
     */
    @Query(value = "select new com.hualude.roadsign.dto.SolarEnergyFaultDto(sf.solarCheck,sf.solarMainCheck,sf.solarMaintenanceStatus) " +
            "from SolarFaultEntity sf where sf.solarEnergyId=?1  ORDER BY sf.solarTime DESC ")
    public List<SolarEnergyFaultDto> getFaultIsReport(Integer solarEnergyId) throws Exception;


    /**
     * @Description: 通过id获取太阳能信息
     * @Author: xu_jiaHui
     * @Date: 2017/7/25 14:28
     */
    public List<SolarEnergyDto> getSolarEnergyById(Integer solarEnergyId) throws Exception;

    /**
     * @Description: 获取太阳能坐标用于地图显示
     * @Author: xu_jiaHui
     * @Date: 2017/7/27 13:41
     */
    public List<SolarEnergyDto> getSolarEnergyCoordinate4Map(Integer orgId, Integer orgType, Integer actorId) throws Exception;

    /**
     * @Description: 通过坐标获取太阳能信息
     * @author xu_jiaHui
     * @date 2017/7/28 10:40
     */
    @Query(value = "select  new com.hualude.roadsign.dto.MapInfoDto(se.solarEnergyId,se.solarEnergyPhoto,se.projectNumber," +
            "se.solarEnergyProvince,se.solarEnergyCity,se.solarEnergyArea,se.solarEnergyRoadName,se.solarEnergyStartRoad," +
            "se.solarEnergyEndRoad,se.solarEnergyType,se.solarEnergyStyle,se.solarEnergyMaterial,se.solarEnergyBrand," +
            "se.solarEnergyInstallTime,se.solarEnergyInstallOrg,se.solarEnergyPeriod,se.solarEnergyServiceLife," +
            "se.solarEnergyRemark,u.userName,o1.orgName,o2.orgName) from SolarEnergyEntity se,OrgEntity o1,OrgEntity o2," +
            "UserEntity u where o1.orgId=se.solarEnergyOrgId and o2.orgId=se.solarEnergyMainOrgId and se.solarEnergyCreatorId=u.userId " +
            "and se.solarEnergyLongitude=?1 and se.solarEnergyLatitude=?2")
    public List<MapInfoDto> getSolarEnergyInfoByXY(String solarEnergyLongitude, String solarEnergyLatitude) throws Exception;


    /**
     * @Description: 根据OrgId, actorId获取城市名4App
     * @author xu_jiaHui
     * @date 2017/7/30 13:28
     */
    public List<String> getCity4SolarEnergy4App(Integer actorId, Integer orgId, Integer mCount, Integer mSize) throws Exception;


    /**
     * @Description: 根据OrgId, actorId,city获取路名4App
     * @author xu_jiaHui
     * @date 2017/7/30 13:25
     */
    public List<String> getRoad4SolarEnergy(Integer actorId, Integer orgId, String city, Integer mCount, Integer mSize) throws Exception;

    /**
     * @Description: 根据OrgId, actorId,roadName获取路名4App
     * @author xu_jiaHui
     * @date 2017/8/2 15:34
     */
    public List<String> getSectionByRoadName4App(String roadName, String solarEnergyRoadName, String city, Integer actorId, Integer orgId, Integer mCount, Integer mSize) throws Exception;

    /**
     * @Description:
     * @author xu_jiaHui
     * @date 2017/8/3 13:31
     */
    public List<String> getStartRoad4App(String roadName, String city, Integer actorId, Integer orgId, Integer mCount, Integer mSize) throws Exception;

    /**
     * @Description: 获取太阳能地图展示信息4App
     * @author xu_jiaHui
     * @date 2017/8/4 15:07
     */
    public List<SolarEnergyDto> getSolarEnergy4Map4App(Integer orgId,
                                                       String actorIds,
                                                       double neLongitude,
                                                       double neLatitude,
                                                       double swLongitude,
                                                       double swLatitude) throws Exception;


    /**
     * @Description: 绑定报警器设备
     * @Author: WYX.
     * @Date: 2018/5/1 19:06.
     */
    @Modifying
    @Query(value = "update SolarEnergyEntity set alarmDeviceId=?1 where solarEnergyId=?2 and alarmDeviceId=null")
    public Integer bindingDevice(String deviceId, Integer solarEnergyId) throws Exception;

    /**
     * @Description: 解绑设备
     * @Author: WYX.
     * @Date: 2018/5/1 19:07.
     */
    @Modifying
    @Query(value = "update SolarEnergyEntity set alarmDeviceId=null where solarEnergyId=?1")
    public Integer unbindingDevice(Integer solarEnergyId) throws Exception;

    public List<SolarEnergyDto> getDeviceInfo(Integer deviceId) throws Exception;

}
